package util.libraries;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import util.BufferedImageModifier;


public abstract class ImageLibrary extends SubDirectoryLibrary
{

    protected ArrayList<BufferedImage> myImages;
    protected Dimension myScalar;


    public ImageLibrary (String dir)
    {
        this(new File(dir));
    }


    public ImageLibrary (File dir)
    {
        this(dir, null);
    }


    public ImageLibrary (File dir, Dimension imagesize)
    {
        super(dir);
        myScalar = imagesize;
        buildLibrary(dir);
    }


    @Override
    public void addToLibrary (Object add)
    {
        try
        {
            if (myScalar == null) myImages.add(ImageIO.read((File) add));
            else myImages.add(BufferedImageModifier.getScaledInstance(ImageIO.read((File) add),
                                                                      myScalar.width,
                                                                      myScalar.height));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        addMappingElement((File) add);
    }


    @Override
    public Object getByID (Object id)
    {
        return myImages.get((Integer) id);
    }


    @Override
    public Object[] getLibrary ()
    {
        return getImageLibrary();
    }


    /**
     * @return
     */
    public BufferedImage[] getImageLibrary ()
    {
        BufferedImage[] i = new BufferedImage[myImages.size()];
        for (int c = 0; c < i.length; c++)
            i[c] = myImages.get(c);
        return i;
    }


//    public Image[] getScaledLibrary(Dimension dimension){
//        return getScaledLibrary(dimension.width, dimension.height);
//    }
//
//    public Image[] getScaledLibrary (int x, int y)
//    {
//        Image[] i = new Image[myImages.size()];
//        for (int c= 0; c < i.length; c++)
//            i[c] = myImages.get(c).getScaledInstance(x, y, 0);
//        return i;
//    }

    @Override
    protected void initialize ()
    {
        myImages = new ArrayList<BufferedImage>();

    }


    protected abstract void addMappingElement (File add);

}
